package l2hc.gameserver.model;

import java.util.concurrent.ScheduledFuture;

import l2hc.commons.threading.RunnableImpl;
import l2hc.gameserver.Config;
import l2hc.gameserver.ThreadPoolManager;
import l2hc.gameserver.ai.CtrlIntention;
import l2hc.gameserver.ai.FakePlayerAI;
import l2hc.gameserver.listener.actor.OnAttackListener;
import l2hc.gameserver.listener.actor.OnMagicUseListener;
import l2hc.gameserver.model.items.ItemInstance;
import l2hc.gameserver.network.serverpackets.CharInfo;
import l2hc.gameserver.templates.item.WeaponTemplate;
import l2hc.gameserver.templates.player.PlayerTemplate;

/**
 * @author ALF
 * @date 12.07.2012
 * 
 *       РџРѕ СЃСѓС‚Рё - СѓРїСЂРѕС‰РµРЅРЅР°СЏ РєРѕРїРёСЏ РїР»РµРµСЂР° РґР»СЏ СЃРєРёР»РѕРІ РљРѕС‚РѕСЂС‹Рµ СЃРїР°РІРЅСЏС‚ РєРѕРїРёСЋ РїР»РµРµСЂР°. Р� РґР»СЏ СЃС‚Р°С‚СѓР№ СЃС‚Р°С‚РёСЃС‚РёРєРё РјСѓР·РµСЏ.
 */
public class FakePlayer extends Creature
{
	private static final long serialVersionUID = -7275714049223105460L;

	private final Player _owner;

	private OwnerAttakListener _listener;

	public FakePlayer(int objectId, PlayerTemplate template, Player owner)
	{
		super(objectId, template);
		_owner = owner;
		_ai = new FakePlayerAI(this);
		_listener = new OwnerAttakListener();
		owner.addListener(_listener);
	}

	@Override
	public Player getPlayer()
	{
		return _owner;
	}

	@Override
	protected void onSpawn()
	{
		super.onSpawn();
		getAI().setIntention(CtrlIntention.AI_INTENTION_FOLLOW, getPlayer(), Config.FOLLOW_RANGE);
	}

	@Override
	public FakePlayerAI getAI()
	{
		return (FakePlayerAI) _ai;
	}

	@Override
	public boolean isAutoAttackable(Creature attacker)
	{
		return false;
	}

	@Override
	public int getLevel()
	{
		return _owner.getLevel();
	}

	@Override
	public ItemInstance getActiveWeaponInstance()
	{
		return _owner.getActiveWeaponInstance();
	}

	@Override
	public WeaponTemplate getActiveWeaponItem()
	{
		return _owner.getActiveWeaponItem();
	}

	@Override
	public ItemInstance getSecondaryWeaponInstance()
	{
		return _owner.getSecondaryWeaponInstance();
	}

	@Override
	public WeaponTemplate getSecondaryWeaponItem()
	{
		return _owner.getSecondaryWeaponItem();
	}

	private ScheduledFuture<?> _broadcastCharInfoTask;

	public class BroadcastCharInfoTask extends RunnableImpl
	{
		@Override
		public void runImpl() throws Exception
		{
			broadcastCharInfoImpl();
			_broadcastCharInfoTask = null;
		}
	}

	@Override
	public void broadcastCharInfo()
	{
		if (_broadcastCharInfoTask != null)
			return;

		_broadcastCharInfoTask = ThreadPoolManager.getInstance().schedule(new BroadcastCharInfoTask(), Config.BROADCAST_CHAR_INFO_INTERVAL);
	}

	public void broadcastCharInfoImpl()
	{
		for (Player player : World.getAroundPlayers(this))
			player.sendPacket(new CharInfo(this));
	}

	public void notifyOwerStartAttak()
	{
		getAI().setIntention(CtrlIntention.AI_INTENTION_ATTACK, _owner.getTarget());
	}

	private class OwnerAttakListener implements OnAttackListener, OnMagicUseListener
	{

		@Override
		public void onMagicUse(Creature actor, Skill skill, Creature target, boolean alt)
		{
			notifyOwerStartAttak();
		}

		@Override
		public void onAttack(Creature actor, Creature target)
		{
			notifyOwerStartAttak();
		}

	}

}
